package com.itheima.tcp1;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

public class Server1 {

    public static void main(String[] args) throws Exception {

        //简单的TCP通信一发一收：服务端开发

        System.out.println("==========服务端启动==========");

        //1.创建一个服务端Socket对象，绑定9999端口，监听客户端连接
        ServerSocket ss = new ServerSocket(9999);

        //2.调用accept()方法，阻塞等待客户端连接，一旦有客户端连接会返回一个Socket对象
        Socket socket = ss.accept();

        //3.从Socket管道中得到一个字节输入流
        InputStream is = socket.getInputStream();

        //4.把字节输入流包装成特殊数据输入流
        DataInputStream dis = new DataInputStream(is);

        //5.读取客户端发送的数据
        int num = dis.readInt();
        String msg = dis.readUTF();
        System.out.println();
        System.out.println("服务端接收到的数据：" + "num = " + num + ",msg = " + msg);
        System.out.println("-----------------------------------------");

        //客户端的IP和端口号
        System.out.println("客户端的IP地址：" + socket.getInetAddress().getHostAddress());
        System.out.println("客户端的端口号：" + socket.getPort());
        System.out.println("-----------------------------------------");

    }
}
